package com.chinaunicom.beanpostprocessor2;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2021/12/9 15:13
 * Description:
 */
public class MyBeanPostProcessor implements BeanPostProcessor , PriorityOrdered , Ordered, MergedBeanDefinitionPostProcessor {

	public MyBeanPostProcessor() {
		System.out.println("bean post processor 初始化。。。");
	}


	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("bean实例化后置处理器开始初始化。。。");

		if (bean instanceof Cat) {
			Cat cat = new Cat();
			cat.setAge("3");
			cat.setGender("m");
			cat.setName("mimi");
			return cat;
		}
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

		System.out.println("bean实例化后置处理器完成初始化。。。");
		System.out.println("beanName info: " + beanName + ", bean info: " + bean);
		return bean;
	}

	public void display() {
		System.out.println("test bean post processor customize。。。");
	}

	@Override
	public int getOrder() {
		return 10;
	}

	@Override
	public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {

	}
}
